package com.sjq.controller;

import com.sjq.model.common.ConstantImg;
import com.sjq.model.common.ResponseBean;
import com.sjq.model.response.ImoocMallExceptionEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;

/**
 * @author: sjq
 * @create: 2022-02-21
 **/
@Api( tags = "上传接口")
@RestController
@RequestMapping("/upload")
public class FileController {

    @ApiOperation(value = "上传图片")
    @PostMapping("/avatar")
    public ResponseBean uploadImg(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            System.out.println("文件为空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        String userpath = System.getProperty("user.dir");
        File dest = new File(userpath+ConstantImg.Upload_File_Dir+fileName);
        File destCopy = new File(userpath+ConstantImg.Upload_File_Dir_Copy+fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
            destCopy.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
//            file.transferTo(destCopy);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            return new ResponseBean(HttpStatus.OK.value(),
                    "successful",
                    getHost(new URI(httpServletRequest.getRequestURL()+""))+"/static/images/"+fileName);
        } catch (URISyntaxException e) {
            return new ResponseBean(ImoocMallExceptionEnum.UPLOAD_IMG.getCode(), ImoocMallExceptionEnum.UPLOAD_IMG.getMsg(), null);
        }
    }

    @ApiOperation(value = "上传文件")
    @PostMapping("/file")
    public ResponseBean uploadFile(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            System.out.println("文件为空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        String userpath = System.getProperty("user.dir");
        File dest = new File(userpath+ConstantImg.Upload_File_Dir+fileName);
        File destCopy = new File(userpath+ConstantImg.Upload_File_Dir_Copy+fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
            destCopy.getParentFile().mkdirs();
        }
        try {
//            FileUtils.copyInputStreamToFile(file.getInputStream(), dest);
            file.transferTo(dest);
//            file.transferTo(destCopy);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            return new ResponseBean(HttpStatus.OK.value(),
                    "successful",
                    getHost(new URI(httpServletRequest.getRequestURL()+""))+"/static/file/"+fileName);
        } catch (URISyntaxException e) {
            return new ResponseBean(ImoocMallExceptionEnum.UPLOAD_IMG.getCode(), ImoocMallExceptionEnum.UPLOAD_IMG.getMsg(), null);
        }
    }

    //    获取IP、端口号
    private URI getHost(URI uri){
        URI effectiveURI;
        try {
            effectiveURI = new URI(uri.getScheme(),uri.getUserInfo(),uri.getHost(),uri.getPort(),null,null,null);
        } catch (URISyntaxException e) {
            effectiveURI=null;
        }
        return effectiveURI;
    }
}

